package com.itboye.ihomebank.activity.key;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.ServerError;
import com.android.volley.TimeoutError;
import com.itboye.ihomebank.R;
import com.itboye.ihomebank.adapter.KaiSuoJiLuAdapter;
import com.itboye.ihomebank.base.BaseOtherActivity;
import com.itboye.ihomebank.base.MyApplcation;
import com.itboye.ihomebank.bean.KaiSuoJiLu;
import com.itboye.ihomebank.bean.KaiSuoJiLuTwo;
import com.itboye.ihomebank.bean.ResultEntity;
import com.itboye.ihomebank.bean.SIDBean;
import com.itboye.ihomebank.constants.SPContants;
import com.itboye.ihomebank.custom.XListView;
import com.itboye.ihomebank.custom.XListView.IXListViewListener;
import com.itboye.ihomebank.interfaces.IRefreshAfterAlert;
import com.itboye.ihomebank.presenter.KeyPresenter;
import com.itboye.ihomebank.presenter.UserPresenter;
import com.itboye.ihomebank.util.ByAlert;
import com.itboye.ihomebank.util.SPUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Observable;
import java.util.Observer;

/**
 * 开锁记录
 */
public class ActivityKaiSuoJiLuOne extends BaseOtherActivity implements Observer ,IXListViewListener{
	View v_statusbar;
	ImageView key_title_finish;
	TextView key_title_title;
	
	KaiSuoJiLuAdapter adapter;

	XListView jilu_listview;
	
	private List<KaiSuoJiLuTwo.KaiSuoJiLuTwoList> beans=new ArrayList<>();
	KeyPresenter keyPresenter;
	
	private String uid;
	
	private String lockId;
	
	private int page;//页码
	private String pageSize = "10";//每页显示的条数
	
	TextView nodata;
	
	ImageView key_title_shezhi;

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.key_title_finish:
			finish();
			break;
		default:
			break;
		}

	}

	@Override
	protected int layout_id() {
		return R.layout.activity_kaisuo_jilu_one;
	}

	@Override
	protected void onInitController() {
		jilu_listview.setXListViewListener(this);
		jilu_listview.setPullLoadEnable(true);
		jilu_listview.setPullRefreshEnable(true);
		
		adapter = new KaiSuoJiLuAdapter(this, beans, R.layout.item_kaisuo_jilu);
		jilu_listview.setAdapter(adapter);
	}

	@Override
	protected void onInit() {
		setStatusBarHeight(v_statusbar);
		key_title_shezhi.setVisibility(View.GONE);
		key_title_title.setText("开锁记录");
		lockId = getIntent().getStringExtra("lockid");
		uid = SPUtils.get(this, null, SPContants.USER_ID, "") + "";
		keyPresenter=new KeyPresenter(this);
		page = 1;
		String sid=SPUtils.get(MyApplcation.ctx,null,SPContants.LOCK_SESSION,"")+"";
		if ("".equals(sid)){
			keyPresenter.getKaiSuoJiLu(uid, lockId, page + "", pageSize);
		}else {
			keyPresenter.getKaiSuoJiLu(sid, lockId, page + "", pageSize);
		}

	}

	@Override
	protected void onReadIntent(Bundle bundle) {
		// TODO Auto-generated method stub

	}

	@Override
	public void update(Observable observable, Object data) {
		ResultEntity entity = handlerError(data);
		if (entity != null) {
			if (entity.getEventType() == KeyPresenter.kaisuo_success) {
				KaiSuoJiLuTwo bean=(KaiSuoJiLuTwo) entity.getData();
				if (page == 1) {// 当页码数为1时，清空，以免下拉时重复加载数据
					beans.clear();
				}
				Log.d("====lock",bean.getData().toString());
				beans.addAll(bean.getData());
				if (beans.size() <= 0) {
					nodata.setVisibility(View.VISIBLE);
					jilu_listview.setVisibility(View.GONE);
				} else {
					nodata.setVisibility(View.GONE);
					jilu_listview.setVisibility(View.VISIBLE);
					adapter.notifyDataSetChanged();
				}
				try {
					stopLoad();
				} catch (Exception e) {
				}
			}else
			if(entity.getEventType() == KeyPresenter.kaisuo_fail){
				try {
					stopLoad();
				} catch (Exception e) {
				}
				if (entity.getData() instanceof ServerError ||entity.getData() instanceof TimeoutError){//服务器失败
					//切换 锁备用服务器
					showAlert();
				}else {
					ByAlert.alert(entity.getMsg());
				}
			} else if (entity.getEventType()== UserPresenter.By_GETSID_success){//获取sid 成功
                SIDBean data1 = (SIDBean) entity.getData();
                SPUtils.put(MyApplcation.ctx,null, SPContants.LOCK_SESSION,data1.getSid());
                SPUtils.put(MyApplcation.ctx,null,SPContants.ISLOCK,true);
                toRefreshData();
            }else if (entity.getEventType()==UserPresenter.By_GETSID_fail){
                ByAlert.alert(entity.getMsg());
            }
		}
	}

	@Override
	public void onRefresh() {
		page = 1;
		keyPresenter = new KeyPresenter(this);
		String sid=SPUtils.get(MyApplcation.ctx,null,SPContants.LOCK_SESSION,"")+"";
		if ("".equals(sid)){
			keyPresenter.getKaiSuoJiLu(uid, lockId, page + "", pageSize);
		}else {
			keyPresenter.getKaiSuoJiLu(sid, lockId, page + "", pageSize);
		}
		
	}

	@Override
	public void onLoadMore() {
		page++;
		keyPresenter = new KeyPresenter(this);
		String sid=SPUtils.get(MyApplcation.ctx,null,SPContants.LOCK_SESSION,"")+"";
		if ("".equals(sid)){
			keyPresenter.getKaiSuoJiLu(uid, lockId, page + "", pageSize);
		}else {
			keyPresenter.getKaiSuoJiLu(sid, lockId, page + "", pageSize);
		}
		
	}
	/**
	 * 停止加载
	 */
	public void stopLoad() {
		jilu_listview.stopLoadMore();
		jilu_listview.stopRefresh();
	}


	private void toRefreshData() {
		String sid=SPUtils.get(MyApplcation.ctx,null,SPContants.LOCK_SESSION,"")+"";
		if ("".equals(sid)){
			keyPresenter.getKaiSuoJiLu(uid, lockId, page + "", pageSize);
		}else {
			keyPresenter.getKaiSuoJiLu(sid, lockId, page + "", pageSize);
		}
	}
}
